﻿using System.Linq;
using Netcaso_MVC.WebUtilities;
using System.Collections.Generic;
using TimKiemViet_VN.DataService;
using System;

namespace TimKiemViet_VN.Models
{
    public class HTMLControlModel
    {
        public HTMLControlModel()
        {
            Locale = MVCLocalization.LocaleKey;
        }

        public HTMLControlModel(int intModuleID)
        {
            ModuleID = intModuleID;
            Locale = MVCLocalization.LocaleKey;
        }
        public HTMLControlModel(int intModuleID, string strLocale)
        {
            ModuleID = intModuleID;
            Locale = strLocale;
        }
        public int ModuleID { get; set; }
        public string Locale { get; set; }

        public string getHTMLControlByModuleID()
        {
            string rs = "";
            HTMLControlDataContext dtContext = new HTMLControlDataContext();
            var obj = dtContext.NCS_HTMLControls.Where(item => item.ModuleID == ModuleID && item.Locale == Locale).FirstOrDefault();
            if (obj != null)
            {
                rs = obj.Content;
            }

            return rs;
        }

        public NCS_HTMLControl HTMLControl
        {
            get
            {
                HTMLControlDataContext dtContext = new HTMLControlDataContext();
                NCS_HTMLControl obj = dtContext.NCS_HTMLControls.Where(item => item.ModuleID == ModuleID && item.Locale == Locale).FirstOrDefault();
                return obj;
            }
        }
        public string Content { get { return this.getHTMLControlByModuleID(); } }

        public List<NCS_HTMLControl> NCS_HTMLControls
        {
            get
            {
                HTMLControlDataContext dtContext = new HTMLControlDataContext();
                return dtContext.NCS_HTMLControls.Where(item => item.Locale == Locale).ToList();
            }
        }

        public bool updateHTMLControlByModuleID(NCS_HTMLControl HTMLControl)
        {
            bool rs = false;
            HTMLControlDataContext dtContext = new HTMLControlDataContext();
            NCS_HTMLControl obj = dtContext.NCS_HTMLControls.Where(item => item.ModuleID == HTMLControl.ModuleID && item.Locale == HTMLControl.Locale).FirstOrDefault();
            if (obj != null)
            {
                obj.Name = HTMLControl.Name;
                obj.Content = HTMLControl.Content;
                obj.CreatedDate = DateTime.Now;
                dtContext.SubmitChanges();
                rs = true;
            }

            return rs;
        }
    }
}
